﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DataService
/// </summary>
public class DataService
{
    private string _connectionstring = "";

    private SqlConnection _conn = null;

    public string ConnectionString
    {
        get
        {
            return _connectionstring;
        }
        set
        {
            _connectionstring = value;
        }
    }

    public DataService()
    {
        _connectionstring = @"Data Source=.;Initial Catalog=chodientu;Integrated Security=True";
    }

    public void Connect()
    {
        try
        {
            _conn = new SqlConnection(_connectionstring);
            _conn.Open();
        }
        catch (Exception e)
        {
            throw new Exception("Không kết nối được CSDL." + e.Message);
        }
    }

    public void Disconnect()
    {
        _conn.Close();
        _conn = null;
    }

    public bool IsConnect()
    {
        return (_conn != null);
    }

    public SqlDataReader ExecuteQuery(string strSQL)
    {
        SqlCommand _command = new SqlCommand(strSQL, _conn);

        return _command.ExecuteReader();

    }

    public object ExecuteScalar(string strSQL)
    {
        SqlCommand _command = new SqlCommand(strSQL, _conn);

        return _command.ExecuteScalar();
    }

    public int ExecuteNoneQuery(string strSQL)
    {

        SqlCommand _command = new SqlCommand(strSQL, _conn);
        int i = _command.ExecuteNonQuery();
        return i;
    }

    public DataSet ExcuteQueryDataSet(string strSQL)
    {
        SqlDataAdapter adapter = new SqlDataAdapter(strSQL, _conn);
        DataSet ds = new DataSet("DATASET");
        adapter.Fill(ds);
        return ds;
    }

    public DataTable ExcuteQueryDataTable(string strSQL)
    {
        DataSet ds = ExcuteQueryDataSet(strSQL);
        return ds.Tables[0];
    }
}
